/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Chapter {
	
	@SuppressWarnings("unused")
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

    @Persistent
	private Integer chapterNo=0;

    @Persistent
	private Integer startVerse=0;

    @Persistent
    private Integer endVerse=0;

    @Persistent
    private String title;

    @Persistent
    private String transliteration;

    @Persistent
	private Integer sectionCount=0;
    
	@Persistent(mappedBy = "chapter")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="startVerse asc"))
    private List<Section> sections;

	public Chapter(int chapterId, int startVerse, int endVerse, String title, String transliteration) {
		this.chapterNo = chapterId;
		this.startVerse = startVerse;
		this.endVerse = endVerse;
		this.title = title;
		this.transliteration = transliteration;
	}
	
	public Chapter() {
	}

	public void setChapterNo(int chapterId) {
		this.chapterNo = chapterId;
	}
	public int getChapterNo() {
		return chapterNo;
	}
	public void setStartVerse(int startVerse) {
		this.startVerse = startVerse;
	}
	public int getStartVerse() {
		return startVerse;
	}
	public void setEndVerse(int endVerse) {
		this.endVerse = endVerse;
	}
	public int getEndVerse() {
		return endVerse;
	}
	public void setTitel(String title) {
		this.title = title;
	}
	public String getTitle() {
		return title;
	}

	private List<Section> getModifiableSections() {
		if (null==sections) {
			sections = new ArrayList<Section>();
		}
		return sections;
	}
	
	public List<Section> getSections() {
		return Collections.unmodifiableList(getModifiableSections());
	}
	
	public Section getSection(int verseNo) {
		for (Section section:sections) {
			if (verseNo==section.getStartVerse()) return section;
		}
		return null;
	}
	
	public void addSection(Section section) {
		section.setChapter(this);
		sectionCount++;
		getModifiableSections().add(section);
	}

	public void removeSection(Section section) {
		getModifiableSections().remove(section);
		sectionCount--;
	}

	public int getSectionCount() {
		return sectionCount;
	}

	public String getTransliteration() {
		return transliteration;
	}

	public void setTransliteration(String transliteration) {
		this.transliteration = transliteration;
	}

}
